package com.example.datlichkham;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.database.TaiKhoanTb;


public class DangKy extends ActionBarActivity {
    String arrTuoi[]={"Tuổi","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16",
            "17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32",
            "33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48",
            "49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64",
            "65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80",
            "81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99","100",">100"};
    String arrGioitinh[]={"Giới tính","Nam","Nữ","Khác"};
    Button btnTrove2,btnDangky;
    EditText edtHoDk, edtTenDk, edtName,edtPass,edtAddress, edtPhone, edtMail;
    //Button btndangnhap1, btnxoahet1, btndangki1, btntrove1;
    String username,password,ho,ten,tuoi,gioitinh,diachi,phone, mail;
    TextView textView3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dang_ky);
        edtHoDk = (EditText) findViewById(R.id.edtHoDK);
        edtTenDk = (EditText) findViewById(R.id.edtTenDK);
        edtName = (EditText) findViewById(R.id.edtName);
        edtPass = (EditText) findViewById(R.id.edtPass);
        edtAddress = (EditText) findViewById(R.id.edtAddress);
        edtPhone = (EditText) findViewById(R.id.edtPhone);
        edtMail = (EditText) findViewById(R.id.edtMail);

        Spinner spinTuoi = (Spinner) findViewById(R.id.spinnerTuoi);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
                arrTuoi
        );
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinTuoi.setAdapter(adapter1);

        Spinner spinGT = (Spinner) findViewById(R.id.spinnerGioitinh);
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
                arrGioitinh
        );
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinGT.setAdapter(adapter2);

        btnTrove2 = (Button) findViewById(R.id.btnTrove2);
        btnTrove2.setOnClickListener(new  View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DangKy.this, MainActivity.class);
                startActivity(intent);
            }
        });
        btnDangky = (Button) findViewById(R.id.btnDangky);
        btnDangky.setOnClickListener(new  View.OnClickListener(){
            @Override
            public void onClick(View v) {
                doValidatedangki();
                dangki();
            }
        });
    }
    public void dangki() {
        String username = edtName.getText().toString();
        String password = edtPass.getText().toString();
        String hokhach = edtHoDk.getText().toString();
        String tenkhach = edtTenDk.getText().toString();
        String address = edtAddress.getText().toString();
        String phone = edtPhone.getText().toString();
        String mail = edtMail.getText().toString();
        TaiKhoanTb taiKhoan = new TaiKhoanTb(DangKy.this);
        long kq = taiKhoan.insertTaiKhoan(username,password,hokhach,tenkhach,tuoi, gioitinh, address, phone, mail);

        if (  kq>0) {
            

            edtName.setText("");
            edtPass.setText("");
            edtHoDk.setText("");
            edtTenDk.setText("");
            edtAddress.setText("");
            edtPhone.setText("");
            edtMail.setText("");
            edtName.requestFocus();

            Toast.makeText(DangKy.this, "Đăng ký thành công !!",
                    Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(DangKy.this, DatLichKham.class);
            startActivity(intent);

        }
        else {
            Toast.makeText(DangKy.this, "Tên đăng nhập này đã có.",
                    Toast.LENGTH_SHORT).show();

        }

    }

    protected boolean doValidatedangki() {
        String username = edtName.getText().toString();
        String password = edtPass.getText().toString();
        String hokhach = edtHoDk.getText().toString();
        String tenkhach = edtTenDk.getText().toString();
        String address = edtAddress.getText().toString();
        String phone = edtPhone.getText().toString();
        String mail = edtMail.getText().toString();
        if (username.equals("") || password.equals("")) {
            //
            Toast.makeText(DangKy.this, "Nhập lại thông tin đầy đủ!!",
                    Toast.LENGTH_SHORT).show();

        }
        if (password.length() <= 6) {

            Toast.makeText(DangKy.this,
                    "Trường pass không ít hơn 6 kí tự!", Toast.LENGTH_SHORT)
                    .show();

        }
        return false;

    }







    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_dang_ky, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
